package com.longcai.cm.utils;

import com.alibaba.fastjson.JSON;
import com.longcai.cm.bean.TreeConfigBean;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CmTreeUtils {


    /**
     * * 将一个列表数据，转换成树形结构  默认取主键字段为id ,父级字段为pid
     *
     * @param originList
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> getCheckedTreeList(Object originList, List<String> checkIds) {
        List<Map<String, Object>> baseList = JSON.parseObject(JSON.toJSONString(originList), List.class);
        return getChild(baseList, new TreeConfigBean(), "", checkIds);
    }

    /**
     * * 将一个列表数据，转换成树形结构  默认取主键字段为id ,父级字段为pid
     *
     * @param originList
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> getCheckedTreeList(Object originList, TreeConfigBean treeConfigBean, List<String> checkIds) {
        List<Map<String, Object>> baseList = JSON.parseObject(JSON.toJSONString(originList), List.class);
        return getChild(baseList, treeConfigBean, "", checkIds);
    }


    private static List<Map<String, Object>> getChild(List<Map<String, Object>> baseList, TreeConfigBean tcb, String pidValue, List<String> checkIds) {
        // 子菜单
        List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();

        // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
        for (Map<String, Object> nav : baseList) {

            if (tcb.getUncheckIdsHiddenFlag()) {

                if (checkIds.contains(nav.get(tcb.getPrimaryKey()))) {

                    if ("".equals(pidValue)) {
                        if (StringUtils.isEmpty(nav.get(tcb.getParentKey()).toString())) {
                            childList.add(nav);
                        }
                    } else {
                        if (nav.get(tcb.getParentKey()).equals(pidValue)) {
                            childList.add(nav);
                        }
                    }

                }

            } else {

                if ("".equals(pidValue)) {
                    if (StringUtils.isEmpty(nav.get(tcb.getParentKey()).toString())) {
                        childList.add(nav);
                    }
                } else {
                    if (nav.get(tcb.getParentKey()).equals(pidValue)) {
                        childList.add(nav);
                    }
                }

                //附加选中状态
                if (checkIds.contains(nav.get(tcb.getPrimaryKey()))) {
                    nav.put("checked", true);
                }
                nav.put("title", nav.get("name"));

            }


        }

        // 递归
        for (Map<String, Object> nav : childList) {
            if (childList.size() > 0) {
                nav.put(tcb.getChildListLabelText(), getChild(baseList, tcb, nav.get(tcb.getPrimaryKey()).toString(), checkIds));
            }
        }
        // 如果节点下没有子节点，返回一个空List（递归退出）
        if (childList.size() == 0) {
            return new ArrayList<Map<String, Object>>();
        }
        return childList;
    }


    /**
     * * 将一个列表数据，转换成树形结构  默认取主键字段为id ,父级字段为pid
     *
     * @param originList
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> getTreeList(Object originList, TreeConfigBean tcb) {
        List<Map<String, Object>> baseList = JSON.parseObject(JSON.toJSONString(originList), List.class);
        return getChild(baseList, tcb, tcb.getParentValue());
    }

    /**
     * * 将一个列表数据，转换成树形结构  默认取主键字段为id ,父级字段为pid
     *
     * @param originList
     * @return
     */
    public static List<Map<String, Object>> getTreeList(Object originList) {
        return getTreeList(originList, "id", "pid");
    }

    /**
     * @param originList
     * @param primaryKey
     * @param parentKey
     * @return
     */
    public static List<Map<String, Object>> getTreeList(Object originList, String primaryKey, String parentKey) {
        return getTreeList(originList, primaryKey, parentKey, "");
    }

    /**
     * 将一个列表数据，转换成树形结构
     *
     * @param originList
     * @param primaryKey
     * @param parentKey
     * @param rootValue
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> getTreeList(Object originList, String primaryKey, String parentKey, String rootValue) {
        List<Map<String, Object>> baseList = JSON.parseObject(JSON.toJSONString(originList), List.class);
        TreeConfigBean tcb = new TreeConfigBean();
        return getChild(baseList, tcb, rootValue);
    }


    private static List<Map<String, Object>> getChild(List<Map<String, Object>> baseList, TreeConfigBean tcb, String pidValue) {
        // 子菜单
        List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();

        // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
        for (Map<String, Object> nav : baseList) {
            if ("".equals(pidValue)) {
                if (StringUtils.isEmpty(nav.get(tcb.getParentKey()).toString())) {
                    childList.add(nav);
                }
            } else {
                if (nav.get(tcb.getParentKey()).equals(pidValue)) {
                    childList.add(nav);
                }
            }
        }

        // 递归
        for (Map<String, Object> nav : childList) {

            List<Map<String, Object>> subChild = getChild(baseList, tcb, nav.get(tcb.getPrimaryKey()).toString());
            if (subChild.size() > 0) {
                nav.put(tcb.getChildListLabelText(), subChild);
            }
        }
        // 如果节点下没有子节点，返回一个空List（递归退出）
        if (childList.size() == 0) {
            return new ArrayList<Map<String, Object>>();
        }
        return childList;
    }


}
